/**
 * Created by ZFL on 2017/11/8.
 */

var idCardInfo={};   //存放身份证信息
var licenceInfo={};    //存放营业执照信息
var navigateImage=[];    //存放门店形象图图片
var logoPath='';    //门店logo图
var serviceImage=[];    //存放商品服务图图片
var status= $.getPara('status');    //店铺状态
PageInit();
function PageInit(){
    classify();     //选择分类
    getAddress();   //选择省市区
    //判断再次进入这个页面的storeId与之前的是否一样且状态为非待上传时    若不一样   删除之前的本地存储   若一样保存
    var storeId= $.getPara('storeId');   //店铺Id
    if($.getStorage('storeId')){
        if(storeId!==$.parseJSON($.getStorage('storeId'))&&status!=='4'){
            $.setStorage('storeId',storeId);
            $.delStorage('idCardInfo');
            $.delStorage('licenceInfo');
            $.delStorage('navigateImage');
            $.delStorage('logoPath');
            $.delStorage('serviceImage');
            $.delStorage('storeInfo');

        }
    }else {
        $.setStorage('storeId',storeId);
    }


}
var vm=new Vue({
    el:'#storeRecruit',
    data:{
        parentCategoryList:[],    //二级分类
        childCategoryList:[],   //三级分类
        categoryParent:'',  //二级分类名称
        category:'',   //三级分类名称
        addressInf:'',         //要编辑的地址
        addressInfo_list:[],
        citysList:[],                 //城市列表
        areasList:[],                 //地区列表
        proSelected:-1,               //选中省时
        citySelected:-1,              //选中市时
        areaSelected:-1,              //选中地区时
        proName:'请选择',              //省名
        cityName:'请选择',             //市名
        areaName:'请选择',             //地区名
        selected:1,
        storeName:'',                 //店铺名称
        tel:'',                        //店铺电话
        shopKeeper:'',                 //店老板手机号
        categoryParentId:'',           //二级分类
        categoryId:'',                 //三级分类
        erSelected:-1,                  //选中二级分类时
        sanSelected:-1,                 //选中三级分类时
        idCard:'',                    //身份证号
        idCardFront:'',                //身份证正面
        idCardReverse:'',              //身份证反面
        businessLicenceName:'',        //营业执照名称
        businessLicence:'',            //营业执照
        idCardHand:'',                  //手持身份证
        province:'',                   //省
        city:'',                       //市
        area:'',                       //区
        address:'',                    //详细地址
        settlementAccount:'',           //结算账户
        settlementBank:'',              //开户支行
        settlementMan:'',               //结算人
        agentUserNum:'',                //介绍人
        shopHours:'',                   //营业时间
        saleBeginTime:'',               //营业开始时间
        saleEndTime:'',                 //营业结束时间
        discount:'',                    //线上买单优惠比例
        discountRate:'',                //线下买单优惠比例
        navigateImage:[],               //门店形象图
        logoPath:'',                    //门店logo
        serviceImage:[],                //门店服务图
        isOnline:0                      //判断店铺是否上线过
    },
    methods:{
        isEdit:function(){
            if(vm.isOnline==1){
                YDUI.dialog.toast('如需更改账号，请联系客服',1000)
            }
        },
        //设置默认选中规格
        isActived:function(){
            var _this = this;
            _this.parentCategoryList.forEach(function(value,index){
                _this.$set(value,'seled',false);
            });


        },
    }
})
$().ready(function(e){
    $("#saleBeginTime").hunterTimePicker();
    $("#saleEndTime").hunterTimePicker();
})


//请选择分类
$('.classify .right div').click(function(e){
    e.preventDefault();
    $(this).siblings('.dropList').slideToggle();
})


//选择二级分类
function SelparentCategory(item,status){
    console.log(item)
    //status用来标明进入重新编辑页面的初始状态
    if(status!==0){
        $('.erji').siblings('.dropList').slideToggle();
        ////当改变二级分类选项时   三级分类的显示随即变化
        //vm.category=item.childCategoryList[0].childCategoryName;
        //vm.categoryId=item.childCategoryList[0].childCategoryId;
    }
    vm.childCategoryList=item.childCategoryList;
    vm.categoryParent=item.parentCategoryName;
    vm.categoryParentId=item.parentCategoryId;
    item.seled=true;
    //当改变二级分类选项时   三级分类的显示随即变化
    vm.category=item.childCategoryList[0].childCategoryName;
    vm.categoryId=item.childCategoryList[0].childCategoryId;
    item.childCategoryList[0].seled=true;
    vm.parentCategoryList.forEach(function(val,ind){
        if(val.parentCategoryId!==item.parentCategoryId){
            val.seled=false;
        }
    })
    console.log(vm.categoryParent)
}

//选择三级分类
function SelchildCategory(item){
    $('.sanji').siblings('.dropList').slideToggle()
    vm.category=item.childCategoryName;
    vm.categoryId=item.childCategoryId;
    item.seled=true;
    vm.childCategoryList.forEach(function(val,ind){
        if(val.childCategoryId!==item.childCategoryId){
            val.seled=false;
        }
    })
}
$('.erji').click(function(){
    $('#cfullbg').show();
    $('body').css('overflow','hidden')
})
$('.sanji').click(function(){
    $('#cfullbg').show();
    $('body').css('overflow','hidden')
})
$('.dropList').click(function(){
    $('#cfullbg').hide();
    $('body').css('overflow','auto')
})

$('#cfullbg').click(function(){
    $('#cfullbg').hide();
    $('.dropList').hide();
    $('body').css('overflow','auto')
})



/*************************地址信息*********************/
$('#address').on('click', function (event){
    event.stopPropagation();
    $('.destination').slideToggle();
    showBg();
});


var address={};
var provances=[] ;    //省份的集合
var citys=[];         //城市的集合
function getAddress(){
    $.ajaxHandler({
        model: function () {
            var obj={};
            return obj;
        },
        visibleTime: 0,
        dataType: "json",
        visible:'hide',
        api: 'yg_baijiaapporder/userAddress/getProvinceCityArea',
        success:function(res){
            if(res.code==10000){
                var data=res.data;
                vm.addressInfo_list=res.data;
                data.forEach(function(value,index){
                    provances.push(value.proName)

                })
            }
        }
    })
}
function classify(){
    $.ajaxHandler({
        model:function(){

        },
        visibleTime: 0,
        dataType: "json",
        approve: true,
//       apiUrl:$.appApi(),
        api: 'yg_baijiaappgoods/category/listStoreCategory',
        success:function(res){
            if(res.code==10000){
                var data=res.data;
                vm.parentCategoryList=data;
                console.log(vm.parentCategoryList)
                vm.isActived();    //设置属性
                if($.getPara('storeId')){
                    reEdit();

                }

            }
        }

    })
}


//点击省   得到所在省的所有城市
function getCitys(c,index){
    vm.citysList= c.citys;
    vm.proSelected=index;
    vm.citySelected=-1;
    vm.proName= c.proName;
    vm.cityName='请选择';
    vm.areaName='请选择';
    vm.selected=2;
}
//得到所在城市的所有区县
function getAreas(a,index){
    vm.areasList=a.areas;
    vm.cityName= a.cityName;
    vm.areaName='请选择';
    vm.citySelected=index;
    vm.areaSelected=-1;
    vm.selected=3;
    $('.destination').css({
        overflow:'visible'
    })
    $('.destination ').animate({
        left:-bw*0.5,
    })

}
//关闭地址联动以及遮罩
function closeDestination(s,index){
    vm.areaSelected=index;
    $('.destination').slideToggle()
    vm.areaName= s.areaName;
    vm.province=vm.proName;
    vm.city=vm.cityName;
    vm.area=vm.areaName;
    vm.addressInf = vm.proName + ' ' + vm.cityName + ' ' + vm.areaName +' ';
    $('#my_inf').show();
    closeBg();
    $('.destination ').animate({
        left:0,
        bottom:0
    })
}

function getSelected(sel){
    vm.selected=sel;
    if(sel==1||sel==2){
        $('.destination ').animate({
            left:0,

        })
    }else if(sel==3){
        $('.destination').animate({
            left:-bw*0.5,

        })
    }else{


    }
}
//获得屏幕的宽高
var bh = $("body").height();
var bw = $("body").width();
//显示灰色 jQuery 遮罩层
function showBg() {
    $("#fullbg").css({
        height:bh,
        width:bw,
        display:"block",
    });
    $('.destinatioWrap').css({
        width:bw*1.5,
    })
    $('.destination .left').css({
        width:bw*0.5
    })
    $('body').css("overflow","hidden");

}
//关闭灰色 jQuery 遮罩
function closeBg(){
    $("#fullbg,#dialog").hide();
    $('body').css("overflow","visible")
}
var storeInfo={};

function uploadImg(n){
    storageInfo();
    switch(n){
        case 0:window.location.href='uploadID.html';   //上传身份证
            break;
        case 1:window.location.href='uploadLicence.html';   //上传营业执照
            break;
        case 2:window.location.href='uploadStFigure.html';   //上传门店形象图
            break;
        case 3:window.location.href='uploadLogo.html';   //上传门店logo
            break;
        case 4:window.location.href='uploadSeImg.html';   //上传商品服务图
            break;
    }
}

////银行卡信息不能修改    点击修改弹出提示信息
//$('.balanceInfo div').click(function(){
//    YDUI.dialog.toast('如需更改账号,请请联系客服',1000)
//})


var srcImg=[];   //存放图片的url
var idCard_img=[];



//重新编辑
function reEdit(){
    $.ajaxHandler({
        model:function(){
            var obj={};
            obj.storeId= $.getPara('storeId');
            return obj;
        },
        visibleTime: 0,
        dataType: "json",
        approve:true,
        api: 'yg_baijiaappgoods/store/getEditStoreInfo',
        success:function(res){
          if(res.code==10000){
              var data=res.data;
              vm.isOnline=data.isOnline;
              if(vm.isOnline==0){
                  //$('#settlementAccount').attr('disabled',false);
                  //$('#settlementBank').attr('disabled',false);
                  //$('#settlementMan').attr('disabled',false);
                  //
                  $('.balanceInfo input').attr('disabled',false)
              }
              showData(data)

          }
        }
    })
}

function showData(data){
        vm.storeName=data.storeName;                //店铺名称
        vm.tel=data.tel;
        vm.shopKeeper=data.shopKeeper;                //店老板手机号
        vm.categoryParentId=data.categoryParentId;           //二级分类
        vm.categoryParent=data.categoryParent;            //二级分类名称
        vm.categoryId=data.categoryId;                //三级分类
        vm.category=data.category;                   //三级分类名称
        setClassify(data.categoryParentId,data.categoryId);
        //idCardInfo.idCard=data.idCard;                  //身份证号
        //idCardInfo.idCardFront=data.idCardFront;                //身份证正面
        //idCardInfo.idCardReverse=data.idCardReverse;              //身份证反面

        //$.setStorage('idCardInfo',idCardInfo);
        licenceInfo.businessLicenceName=data.businessLicenceName;       //营业执照名称
        licenceInfo.businessLicence=data.businessLicence;           //营业执照
        licenceInfo.idCardHand=data.idCardHand;                    //手持身份证
        vm.province=data.province;                   //省
        vm.city=data.city;                       //市
        vm.area=data.area;                      //区
        vm.address=data.address;                  //详细地址
        vm.settlementAccount=data.settlementAccount;          //结算账户
        vm.settlementBank=data.settlementBank;              //开户支行
        vm.settlementMan=data.settlementMan;              //结算人
        vm.agentUserNum=data.agentUserNum;
        //介绍人
        $('#saleBeginTime').val(data.saleBeginTime);
        $('#saleEndTime').val(data.saleEndTime);
        vm.discount=data.discount;                   //线上买单优惠比例
        vm.discountRate=data.discountRate;                //线下买单优惠比例
        navigateImage=data.navigateImage;               //门店形象图

        logoPath=data.logoPath;                   //门店logo

        serviceImage=data.serviceImage;           //商品服务图

        vm.addressInf=data.province+' '+data.city+' '+data.area;   //省市区



        //当状态为非上传时  若检测到没有本地存储则设置    若检测到则不设置   以防和上传身份证页面   上传营业执照页面  等冲突
        if(status!=4){
           if(!$.getStorage('idCardInfo')){
               $.setStorage('idCardInfo',idCardInfo);
           }

           if(!$.getStorage('licenceInfo')){
               $.setStorage('licenceInfo',licenceInfo);
           }
           if(!$.getStorage('navigateImage')){
               $.setStorage('navigateImage',navigateImage);
           }
           if(!$.getStorage('logoPath')){
               $.setStorage('logoPath',logoPath);
           }
           if(!$.getStorage('serviceImage')){
               $.setStorage('serviceImage',serviceImage);
           }

       }

    if($.getStorage('storeInfo')){

        vm.storeName=$.parseJSON($.getStorage('storeInfo')).storeName;
        vm.tel=$.parseJSON($.getStorage('storeInfo')).tel;
        vm.categoryParent=$.parseJSON($.getStorage('storeInfo')).categoryParent;
        vm.category=$.parseJSON($.getStorage('storeInfo')).category;
        vm.parentCategoryList.forEach(function(value,index){
            if(value.parentCategoryName==vm.categoryParent){
                value.childCategoryList.forEach(function(val,ind){
                    if(val.childCategoryName==vm.category){
                        setClassify(value.categoryParentId,val.categoryId)
                    }

                })

            }

        })


        vm.province=$.parseJSON($.getStorage('storeInfo')).province;                   //省
        vm.city=$.parseJSON($.getStorage('storeInfo')).city;                       //市
        vm.area=$.parseJSON($.getStorage('storeInfo')).area;                      //区
        vm.address=$.parseJSON($.getStorage('storeInfo')).address;                  //详细地址

        vm.addressInf =vm.province + ' ' + vm.city+ ' ' + vm.area +' ';

        vm.settlementAccount=$.parseJSON($.getStorage('storeInfo')).settlementAccount;
        vm.settlementBank=$.parseJSON($.getStorage('storeInfo')).settlementBank;
        vm.settlementMan=$.parseJSON($.getStorage('storeInfo')).settlementMan;

        vm.saleBeginTime=$.parseJSON($.getStorage('storeInfo')).saleBeginTime;
        vm.saleEndTime=$.parseJSON($.getStorage('storeInfo')).saleEndTime;

        $('#saleBeginTime').val($.parseJSON($.getStorage('storeInfo')).saleBeginTime);
        $('#saleEndTime').val($.parseJSON($.getStorage('storeInfo')).saleEndTime);

        vm.discount=$.parseJSON($.getStorage('storeInfo')).discount;
        vm.discountRate=$.parseJSON($.getStorage('storeInfo')).discountRate;
    }
    isUpload();


}
//设置二级分类  三级分类ui
function setClassify(categoryParentId,categoryId){
    vm.parentCategoryList.forEach(function(value,index){
        if(value.parentCategoryId==categoryParentId){
            value.seled=true;
            SelparentCategory(value,0);//0用来区分是重新编辑时  页面的初始状态
            value.childCategoryList.forEach(function(val,ind){
                if(val.childCategoryId==categoryId){
                    val.seled=true;
                }
            })
        }

    })

}
//判断身份证  营业执照  门店形象图   门店logo   商品服务图上传后的ui展示
function isUpload(){
    if($.getStorage('idCardInfo')){
        $('.uploadId .prompt').html('已上传法人身份证')
    }

    if($.getStorage('licenceInfo')&&$.parseJSON($.getStorage('licenceInfo')).businessLicenceName!=''&&$.parseJSON($.getStorage('licenceInfo')).businessLicence!=''&&$.parseJSON($.getStorage('licenceInfo')).idCardHand!=''){
        $('.uploadLicence .prompt').html('已上传营业执照')
    }
    if(JSON.parse($.getStorage('navigateImage')).length!==0){
        $('.uploadFigure div').css({
            border: '1px solid #fd4568'
        })
        $('.uploadFigure button').text('已上传图片')
    }

    if($.getStorage('logoPath')){
            $('.uploadLogo div').css({
                border:'1px solid #fd4568'
            })
        $('.uploadLogo button').text('已上传图片')

    }
    if(JSON.parse($.getStorage('serviceImage')).length!==0){
                $('.uploadServer div').css({
                    border:'1px solid #fd4568'
                })
        $('.uploadServer button').text('已上传图片')
    }
}
var userId= $.getPara('userId');




//存储店铺信息
function  storageInfo(){
    storeInfo.storeName=vm.storeName;
    storeInfo.tel=vm.tel;

    storeInfo.categoryParent=vm.categoryParent;
    storeInfo.category=vm.category;

    storeInfo.province=vm.province;
    storeInfo.city=vm.city;
    storeInfo.area=vm.area;
    storeInfo.address=vm.address;

    storeInfo.settlementAccount=vm.settlementAccount;
    storeInfo.settlementBank=vm.settlementBank;
    storeInfo.settlementMan=vm.settlementMan;

    storeInfo.saleBeginTime=$('#saleBeginTime').val();
    storeInfo.saleEndTime=$('#saleEndTime').val();
    console.log($('#saleBeginTime').val())
    storeInfo.discount=vm.discount;
    storeInfo.discountRate=vm.discountRate;

    $.setStorage('storeInfo',storeInfo)


}
//点击提交按钮
$('.submitBtn').click(function(e){
    e.preventDefault();

    if($("input#storeName").val()!=''&&$("input#tel").val()!=''&&$("input#storeName").val()!=''&&$("input#categoryParent").val()!=''&&$("input#category").val()!=''&&$("input#addressInf").val()!=''&&$("input#detailAddress").val()!=''&&$("input#settlementAccount").val()!=''&&$("input#settlementBank").val()!=''&&$("input#settlementMan").val()!=''&&$('input#agentUserNum').val()!=''&&$('input#saleBeginTime').val()!=''&&$('input#saleEndTime').val()!=''&&$('input#discount').val()!=''&&$('input#discountRate').val()!=''&&$.getStorage('licenceInfo')&&JSON.parse($.getStorage('licenceInfo')).idCardHand!=''&&$.getStorage('navigateImage')&&$.getStorage('logoPath')&&$.getStorage('serviceImage')&&JSON.parse($.getStorage('serviceImage')).length!==0&&JSON.parse($.getStorage('navigateImage')).length!==0&&JSON.parse($.getStorage('logoPath')).length!==0){
        if($('input#discount').val()>90||$('input#discount').val()<0||$('input#discountRate').val()<0||$('input#discountRate').val()>100){
            YDUI.dialog.toast('请按要求填写优惠比例',1000)
        }else{
            submitEditData()
        }
        
    }else if($("input#storeName").val()==''){
        YDUI.dialog.toast('请填写店铺名称',1000)
    }else if($("input#tel").val()==''){
        YDUI.dialog.toast('请填写店铺电话',1000)
    }else if($("input#categoryParent").val()==''){
        YDUI.dialog.toast('请选择二级分类',1000)
    }else if($("input#category").val()==''){
        YDUI.dialog.toast('请选择三级分类',1000)
    }else if($("input#addressInf").val()==''||$("input#detailAddress").val()==''){
        YDUI.dialog.toast('请将地址信息填写完整',1000)
    }else if($("input#settlementAccount").val()==''){
        YDUI.dialog.toast('请填写银行卡号',1000)
    }else if($("input#settlementBank").val()==''){
        YDUI.dialog.toast('请填写开户银行名',1000)
    }else if($("input#settlementMan").val()==''){
        YDUI.dialog.toast('请填写银行卡户名',1000)
    }else if($("input#saleBeginTime").val()==''){
        YDUI.dialog.toast('请填写营业开始时间',1000)
    }else if($("input#saleEndTime").val()==''){
        YDUI.dialog.toast('请填写营业结束时间',1000)
    }else if($("input#discount").val()==''){
        YDUI.dialog.toast('请设置线下买单优惠比例',1000)
    }else if($("input#discountRate").val()==''){
        YDUI.dialog.toast('请设置线上买单优惠比例',1000)
    }else if(SON.parse($.getStorage('licenceInfo')).idCardHand==''){
        YDUI.dialog.toast('请将营业执照信息填写完整',1000)
    }
})
//重新编辑后提交信息
function submitEditData(){
    $.ajaxHandler({
        model:function(){
            var obj={};
            obj.storeName=vm.storeName;                //店铺名称
            obj.tel=vm.tel;                      //店铺电话
            obj.shopKeeper=vm.shopKeeper;                //店老板手机号
            obj.categoryParentId=vm.categoryParentId;           //二级分类
            obj.categoryId=vm.categoryId;                //三级分类
            //obj.idCard=$.parseJSON($.getStorage('idCardInfo')).idCard;                  //身份证号
            //obj.idCardFront=$.parseJSON($.getStorage('idCardInfo')).idCardFront;                //身份证正面
            //obj.idCardReverse=$.parseJSON($.getStorage('idCardInfo')).idCardReverse;              //身份证反面
            obj.businessLicenceName=$.parseJSON($.getStorage('licenceInfo')).businessLicenceName;       //营业执照名称
            obj.businessLicence=$.parseJSON($.getStorage('licenceInfo')).businessLicence;           //营业执照
            obj.idCardHand=$.parseJSON($.getStorage('licenceInfo')).idCardHand;                    //手持身份证
            obj.province=vm.province;                   //省
            obj.city=vm.city;                       //市
            obj.area=vm.area;                      //区
            obj.address=vm.address;                    //详细地址
            obj.settlementAccount=vm.settlementAccount;          //结算账户
            obj.settlementBank=vm.settlementBank;              //开户支行
            obj.settlementMan=vm.settlementMan;              //结算人
            obj.agentUserNum=vm.agentUserNum;               //介绍人
            obj.saleBeginTime=$('#saleBeginTime').val();               //营业开始时间
            obj.saleEndTime=$('#saleEndTime').val();               //营业结束时间
            obj.discount=vm.discount;                   //线上买单优惠比例
            obj.discountRate=vm.discountRate;                //线下买单优惠比例
            obj.navigateImage=$.parseJSON($.getStorage('navigateImage'));               //门店形象图
            obj.logoPath=$.parseJSON($.getStorage('logoPath'));                    //门店logo
            obj.serviceImage=$.parseJSON($.getStorage('serviceImage'));               //门店服务图
            obj.storeId= $.getPara('storeId');
            console.log(obj)
            return obj;
        },
        visibleTime: 0,
        dataType: "json",
        approve: true,
        api: 'yg_baijiaappgoods/store/updateEditStoreInfo',
        success:function(res){
            if(res.code=10000){
                YDUI.dialog.toast(res.message,'success',1000)
                //提交成功后   删除本地存储信息
                $.delStorage('idCardInfo');
                $.delStorage('licenceInfo');
                $.delStorage('navigateImage');
                $.delStorage('logoPath');
                $.delStorage('serviceImage');
                $.delStorage('storeInfo');

                $('#fullbg').show();
                $('.tishi').show()

                //var url='./mySquadron.html?userId='+userId+'&Android=Android';
                //setTimeout(function(){
                //    window.location.href=url;
                //},2000);
            }else{
                YDUI.dialog.toast(result.message,'error',1000)
            }
        }

    })


}



//点击下载云店   与app进行交互
function jumpToApp(){
    var u = navigator.userAgent;
    var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端
    var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
    var url='';
    var scheme='';
    if(isAndroid){
        url='http://sj.qq.com/myapp/detail.htm?apkName=com.glela.huashengyundian'
        //alert("安卓系统");
        JSAndroid.toJumpOtherApp(url);
    }else if(isiOS){
        url='1330075403';
        scheme='wistomStore';
        //window.webkit.messageHandlers.shareBooking.postMessage(this.data.activity.id,this.data.activity.join_company_id);
        toJumpOtherApp(url,scheme);
    }
}


$('#fullbg').click(function(){
    $('.tishi').hide();
    $('#fullbg').hide();
})



















